// get 请求

var http = require('http');
var querystring = require('querystring');
var url = require('url');


var _from = `<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <form action="" method="post">
    <p>姓名: <input type="text" name="name"></p>
    <p>年龄: <input type="text" name="age"></p>
    <input type="submit" value="提交">
  </form>
</body>
</html>`;
http.createServer(function(req,res){
  var body = '';
  req.on('data',function(_d){
    body += _d;
  });

  req.on('end',function(){
    // 解析请求字符串(类似 : a=b&b=c ) 为对象
    body = querystring.parse(body);
    console.log(body);
    res.writeHead(200,{
      'Content-Type':'text/html;charset=utf-8'
    });

    if(body.name){
      res.write("姓名: " + body.name + "<br />");
      res.write("age: " + body.age);
    }else{
      res.write(_from);
    }

    res.end();
  });

}).listen(5642);

console.log('5642 is listening');